package com.bookpavilion.search.controller;

import com.bookpavilion.search.pojo.Goods;
import com.bookpavilion.search.service.GoodsService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping()
    public Result findAll(){
        List<Goods> goods = goodsService.findAll();
        return new Result(true,StatusCode.OK,"查询成功",goods);

    }
    @PostMapping()
    public Result save(@RequestBody Goods goods){
        goodsService.save(goods);
        return new Result(true, StatusCode.OK,"增加成功");
    }

    @GetMapping("/{keyword}/{page}/{size}")
    public Result findByKey(@PathVariable String keyword,@PathVariable int page,@PathVariable int size){
        Page<Goods> pageData =goodsService.findByKey(keyword,page,size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<Goods>(pageData.getTotalElements(),pageData.getContent()));
    }

    @GetMapping("/search/{categoryId}/{page}/{size}")
    public Result findByCategoryId(@PathVariable String categoryId,@PathVariable int page,@PathVariable int size){
        Page<Goods> pageData = goodsService.findByCategoryId(categoryId, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<Goods>(pageData.getTotalElements(),pageData.getContent()));
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        goodsService.delete("id");
        return new Result(true,StatusCode.OK,"删除成功");
    }

}
